package book;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;

public class Book implements Comparable<Book>{
    //成员属性
    private int bookId;            //书籍ID
    private String title;          //书名
    private String auther;         //作者
    private String category;       //类型
    private int publishYear;       //出版年份
    private boolean isBorrowed;    //借阅状态
    private int brorrowCount;      //借阅次数
    private LocalDate shelfDatel;  //上架时间

    //构造方法
    public Book(String title, String auther, String category, int publishYear, LocalDate shelfDatel) {
        this.title = title;
        this.auther = auther;
        this.category = category;
        this.publishYear = publishYear;
        this.shelfDatel = shelfDatel;
    }

    //get方法
    public int getBookId() {
        return bookId;
    }

    public String getTitle() {
        return title;
    }

    public String getAuther() {
        return auther;
    }

    public String getCategory() {
        return category;
    }

    public int getPublishYear() {
        return publishYear;
    }

    public boolean getIsBorrowed() {
        return isBorrowed;
    }

    public int getBrorrowCount() {
        return brorrowCount;
    }

    public LocalDate getShelfDatel() {
        return shelfDatel;
    }

    //set方法
    public void setBorrowCount(int brorrowCount) {
        this.brorrowCount = brorrowCount;
    }

    public void setBorrowed(boolean borrowed) {
        isBorrowed = borrowed;
    }

    public void setCategory(String category) {
        this.category = category;
    }

    public void setAuther(String auther) {
        this.auther = auther;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public void setBookId(int bookId) {
        this.bookId = bookId;
    }

    public void setPublishYear(int publishYear) {
        this.publishYear = publishYear;
    }

    public void setShelfDatel(LocalDate shelfDatel) {
        this.shelfDatel = shelfDatel;
    }

    //重写toString方法
    @Override
    public String toString() {
        return "Book{" +
                "书籍编号：" + bookId +
                ", 书名：" + title + '\'' +
                ", 作者：" + auther + '\'' +
                ", category='" + category + '\'' +
                ", 出版年份：" + publishYear +
                ", 借阅状态：=" + (isBorrowed == true? "已借阅" : "未借阅") +
                ", 借阅次数：" + brorrowCount +
                ", 上架时间：" + shelfDatel +
                '}';
    }

    //toJSON方法
    public String toJSON(){
        //拼接
        StringBuilder toJSON = new StringBuilder();
        toJSON.append(bookId).append(",");
        toJSON.append(title).append(",");
        toJSON.append(auther).append(",");
        toJSON.append(category).append(",");
        toJSON.append(publishYear).append(",");
        toJSON.append(isBorrowed).append(",");
        toJSON.append(brorrowCount).append(",");
        toJSON.append(shelfDatel != null ? shelfDatel.format(DateTimeFormatter.ISO_LOCAL_DATE) : "null");
        //返回字符串
        return toJSON.toString();
    }


    @Override
    public int compareTo(Book o) {
        return o.getBrorrowCount() - this.getBrorrowCount();
    }
}
